<template>
  <div>
    <sub-title :subTitle="'支付设置'" :isRefresh="false"></sub-title>
    <div class="body">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="form">
        <el-form-item label="支付名称: " prop="name">
          <el-input v-model="ruleForm.name "></el-input>
        </el-form-item>
        <el-form-item label="支付描述: " prop="detail">
          <el-input type="textarea" v-model="ruleForm.detail"></el-input>
        </el-form-item>
        <el-form-item label="AppId: " prop="appid">
          <el-input v-model="ruleForm.appid"></el-input>
        </el-form-item>
        <el-form-item label="AppSecret: " prop="appsecret">
          <el-input v-model="ruleForm.appsecret"></el-input>
        </el-form-item>
        <el-form-item label="商户密钥key: " prop="appkey">
          <el-input v-model="ruleForm.appkey"></el-input>
        </el-form-item>
        <el-form-item label="支付商户号: " prop="mchId">
          <el-input v-model="ruleForm.mchId"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import subTitle from "@/components/subTitle"
  import mixin from "@/utils/mixin"
  export default {
    name: "pay",
    mixins: [mixin],
    components: {
      subTitle,
    },
    data(){
      return{
        rules: {
          name: [
            { required: true, message: '请输入支付名称', trigger: 'blur' },
            { max: 20, message: '长度必须小于20个字符', trigger: 'blur' }
          ],
          appid: [
            { required: true, message: '请输入AppID', trigger: 'blur' },
          ],
          appsecret: [
            { required: true, message: '请输入AppSecret', trigger: 'blur' },
          ],
          appkey: [
            { required: true, message: '请输入商户密钥key', trigger: 'blur' },
          ],
          mchId: [
            { required: true, message: '请输入支付商户号', trigger: 'blur' },
          ],
        },
        ruleForm: {
          name: '',
          detail: '',
          appid: '',
          appsecret: '',
          appkey: '',
          mchId: '',
        }
      }
    },
    created(){
    },
    mounted(){
      this.$http.post("merchant_pay_mode/query").then((res) => {
        this.$set(this.ruleForm, 'name', res.name);
        this.$set(this.ruleForm, 'detail', res.detail);
        this.$set(this.ruleForm, 'appid', res.appid);
        this.$set(this.ruleForm, 'appsecret', res.appsecret);
        this.$set(this.ruleForm, 'appkey', res.appkey);
        this.$set(this.ruleForm, 'mchId', res.mchId);
        if (res.id){
          this.$set(this.ruleForm, 'id', res.id);
        }
      }, (err) => {
        this.$msgErr(err.msg);
      })
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.ruleForm.id){
              this.addEdit("merchant_pay_mode/update");
            } else {
              this.addEdit("merchant_pay_mode/add");
            }
          } else {
            return false;
          }
        });
      },
      addEdit(url){
        this.$http.post(url, this.ruleForm).then(() => {
          this.$msgSuc("操作成功");
        }, (err) => {
          this.$msgErr(err.msg);
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  @import "~@/assets/css/common";
  .form{
    width: 600px;
    .form-tips{
      font-size: 12px;
      color: $gray;
    }
  }
  .upload-input{
    position: absolute;
    opacity: 0;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    z-index: 2;
  }
  .form-tips{
    font-size: 12px;
    color: $gray;
    line-height: 16px;
    margin-top: 5px;
  }
  .form-select{
    margin-bottom: 20px;
    &:last-child{
      margin-bottom: 0;
    }
    .el-select{
      width: 170px;
      margin-right: 20px;
    }
    span{
      margin-right: 10px;
      cursor: pointer;
    }
  }
</style>